package cn.kgc.service.impl;

import cn.kgc.client.CityClient;
import cn.kgc.client.ClazzClient;
import cn.kgc.client.GroupClient;
import cn.kgc.client.TagClient;
import cn.kgc.dao.StudentDao;
import cn.kgc.dao.StudentTagDao;
import cn.kgc.dto.StudentRequestDTO;
import cn.kgc.dto.StudentResponseDTO;
import cn.kgc.entity.*;
import cn.kgc.service.StudentService;
import cn.kgc.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mosin
 * date 2022/1/17
 * @version 1.0
 */
@Service
public class StudentServiceImpl  implements StudentService {
    @Autowired
    private StudentDao studentDao;

    //clazz客户端
    @Autowired
    private ClazzClient clazzClient;

    //小组客户端
    @Autowired
    private GroupClient groupClient;

    //城市客户端
    @Autowired
    private CityClient cityClient;

    //标签客户端
    @Autowired
    private TagClient tagClient;

    //查询所有标签id的  StudentTagDao
    @Autowired
    private StudentTagDao studentTagDao;

    @Override
    public List<StudentResponseDTO> queryAll() {
        //创建集合   存放返回的数据
        List<StudentResponseDTO> studentDTOS = new ArrayList<>();
        List<Student>  students =  studentDao.queryAll();
        students.forEach(student -> {
            System.out.println(student);
        });

        //遍历集合  获取 城市 小组  标签的id
        students.forEach(student -> {
            StudentResponseDTO studentDTO = new StudentResponseDTO();
            //将student对象 转换为studentDTO对象
            BeanUtils.copyProperties(student, studentDTO);

            //根据班级id  查询班级信息  调用班级微服务
            Clazz clazz = clazzClient.getClazz(Integer.parseInt(student.getClazzId()));
            studentDTO.setClazz(clazz);

            //根据小组id  查询小组信息  调用小组微服务
            Group group = groupClient.group(Integer.parseInt(student.getGroupId()));
            studentDTO.setGroup(group);

            //根据城市id  查询城市信息 调用城市微服务
            City city = cityClient.city(Integer.parseInt(student.getCityId()));
            System.out.println(city);

            studentDTO.setCity(city);

            //查询标签对象  学生 标签  多对多   1.根据学生id 查询学生对应 tid  2.根据tid 查询标签信息
            List<Integer> tids = studentTagDao.queryAll(student.getId());
            //遍历标签集合  调用标签微服务  查询标签信息
            tids.forEach(tid->{
                studentDTO.getTags().add(tagClient.getOne(tid));
            }) ;

            //添加studentDTO 到集合
            studentDTOS.add(studentDTO);
        });

        return studentDTOS;
    }

    @Override
    public StudentResponseDTO insert(StudentRequestDTO studentRequestDTO) {

        //StudentRequestDTO - > Student
        Student student = new Student();
        BeanUtils.copyProperties(studentRequestDTO,student);
        //通过工具类 计算学生的年龄 生肖  星座
        student.setAge(DateUtil.getAge(student.getBir()));
        student.setAttr(DateUtil.getYear(student.getBir()));
        student.setStarts(DateUtil.getConstellation(student.getBir()));

        studentDao.addStudent(student);

        //student对象中已经包含自增主键id值  18
        long sid = student.getId();
        List<Integer> tagIds = studentRequestDTO.getTagIds();
        //遍历集合 创建 StudentTag对象  插入StudentTag表
        tagIds.forEach(tid->{
            studentTagDao.addStudentTag(new StudentTag(sid, tid));
        });

        //将student对象 -> studentResponseDTO

        StudentResponseDTO studentDTO = new StudentResponseDTO();
        BeanUtils.copyProperties(student, studentDTO);

        //根据班级id  查询班级信息  调用班级微服务
        Clazz clazz = clazzClient.getClazz(Integer.parseInt(student.getClazzId()));
        studentDTO.setClazz(clazz);

        //根据小组id  查询小组信息  调用小组微服务
        Group group = groupClient.group(Integer.parseInt(student.getGroupId()));
        studentDTO.setGroup(group);

        //根据城市id  查询城市信息 调用城市微服务
        City city = cityClient.city(Integer.parseInt(student.getCityId()));
       studentDTO.setCity(city);

        //遍历标签集合 调用标签微服务  查询标签id对应的对象

        tagIds.forEach(tid->{
            studentDTO.getTags().add( tagClient.getOne(tid));
        });
        return studentDTO;
    }
}
